package com.hpin.mlxteacher.utils;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.hpin.mlxteacher.base.MyApplication;
import com.hpin.mlxteacher.bean.UploadBean;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;

import java.util.HashMap;
import java.util.Map;

/**
 * 七牛上传图片
 * Created by Aaron on 16/4/7.
 */
public class QiniuUtils {

    public static void getQiniuPic(final byte[] bytes, final Application mApplication, String token, final QiniuSuccessListener qiniuListener) {

        final JSONObject json = new JSONObject();
        json.put("token", token);

        //请求七牛, 上传图片
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.WEB_ROOT + Constant.UPLOADTOKEN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                final UploadBean uploadBean = JSONObject.parseObject(response, UploadBean.class);

                if (uploadBean.code == Constant.SUCCESS) {

                    UploadManager uploadManager = new UploadManager();

                    uploadManager.put(bytes, uploadBean.path, uploadBean.token,
                            new UpCompletionHandler() {
                                @Override
                                public void complete(String s, ResponseInfo responseInfo, org.json.JSONObject jsonObject) {

                                    if (responseInfo.isOK()) {

                                        if (qiniuListener != null) {
                                            qiniuListener.setOnSuccessListener(uploadBean.domain + uploadBean.path);
                                        }

                                        //图片上传成功
                                        LogUtil.e("tag", "图片的地址:======" + uploadBean.domain + uploadBean.path);

                                    } else {
                                        LogUtil.e("tag", "图片上传失败");

                                        qiniuListener.setOnSuccessListener(Constant.IMAGE_URL_FAIL);

                                    }

                                }

                            }, null);

                } else {
                    Toast.makeText(mApplication, Constant.showErrorMsg(uploadBean.code, mApplication), Toast.LENGTH_SHORT);
                }

            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                qiniuListener.setOnSuccessListener(Constant.IMAGE_URL_FAIL);
            }

        }) {

            protected Map<String, String> getParams() {
                Map<String, String> param = new HashMap<String, String>();
                param.put("data", JSON.toJSONString(json, SerializerFeature.DisableCircularReferenceDetect));
                return param;
            }

        };

        //把请求加入队列
        ((MyApplication) mApplication).addToRequestQueue(stringRequest, "QiniuUpload");

    }


    /**
     * 获取网络图片,
     *
     * @param mContext
     * @param url
     * @param picListener
     */
    public static void getBitmap(Context mContext, String url, final PicSuccessListener picListener) {

        ImageRequest imageRequest = new ImageRequest(
                url,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {

                        if (picListener != null)
                            picListener.setOnSuccessListener(response);

                    }
                }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (picListener != null)
                    picListener.setOnSuccessListener(null);

            }
        });

        //把请求加入队列
        ((MyApplication) mContext.getApplicationContext()).getRequestQueue().add(imageRequest);

    }


    public interface QiniuSuccessListener {

        void setOnSuccessListener(String str);

    }

    public interface PicSuccessListener {

        void setOnSuccessListener(Bitmap bp);

    }

//    public interface PicFailListener {
//
//        void setOnFailListener();
//
//    }


}
